﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace encrpt
{
    public class RSA
    {
        public static bool CreateKey()
        {
            try
            {
                var rsaKey = new RSACryptoServiceProvider();
                string publickey = rsaKey.ToXmlString(false);
                string privatekey = rsaKey.ToXmlString(true);
                File.WriteAllText("private.xml", privatekey, Encoding.UTF8);
                File.WriteAllText("public.xml", publickey, Encoding.UTF8);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static void LoadKey()
        {
            try
            {
                Privatexml = File.ReadAllText("private.xml", Encoding.UTF8);
            }
            catch (Exception ex)
            {
                return;
            }
            try
            {
                Publicxml = File.ReadAllText("public.xml", Encoding.UTF8);
            }
            catch (Exception ex)
            {
                return;
            }
        }

        protected static string Publicxml { get; set; }
        protected static string Privatexml { get; set; }


        static bool _isprivXml = true;
        static bool _ispubXml = true;

        public static string Encrypt(string text)
        {
            byte[] data;
            var rsa = new RSACryptoServiceProvider();
            try
            {
                if (Publicxml.Length == 0)
                {
                    _ispubXml = false;
                    return null;
                }
                rsa.FromXmlString(Publicxml);
            }
            catch (Exception e)
            {
                return e.Message;
            }
            try
            {
                data = Encoding.UTF8.GetBytes(text);
            }
            catch (Exception e)
            {
                return e.Message;
            }
            try
            {
                EncryptedData = rsa.Encrypt(data, false);
            }
            catch (CryptographicException e)
            {
                MessageBox.Show(e.Message);
            }
            string result = Convert.ToBase64String(EncryptedData);
            for (int i = 0; i < data.Length - 1; i++)
            {
                data.SetValue((byte)0, i);
            }
            for (int i = 0; i < EncryptedData.Length - 1; i++)
            {
                EncryptedData.SetValue((byte)0, i);
            }
            return result;
        }
        public static string Decrypt(string text)
        {
            var data = new byte[] {};
            var rsa = new RSACryptoServiceProvider();
            try
            {
                if (Privatexml.Length == 0)
                {
                    _isprivXml = false;
                    return null;
                }
                rsa.FromXmlString(Privatexml);
            }
            catch (Exception)
            {
                
            }
            try
            {
                data = Convert.FromBase64String(text);
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show(e.Message);
            }
            try
            {
                DecryptedData = rsa.Decrypt(data, false);
            }
            catch (CryptographicException)
            {
                return null;
            }
            string result = Encoding.UTF8.GetString(DecryptedData);
            for (int i = 0; i < data.Length - 1; i++)
            {
                data.SetValue((byte)0, i);
            }
            for (int i = 0; i < DecryptedData.Length - 1; i++)
            {
                DecryptedData.SetValue((byte)0, i);
            }
            return result;
        }

        protected static byte[] DecryptedData { get; set; }
        protected static byte[] EncryptedData { get; set; }
    }
}
